//
//  CZComposeToolBar.swift
//  微博
//
//  Created by 霸气的刘吉庆 on 16/4/25.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit

//定义一个枚举
enum CZComposeToolBarButtonType: Int {

    case Picture = 0, Mention, Trend, Emoticon, Add
    
    
}
class CZComposeToolBar: UIStackView {

    var isSystemKeyBoard: Bool = true{
        //设置按钮状态,
        
        didSet{
            
            //根据tag获取对应的button的值
            
            let button = self.viewWithTag((CZComposeToolBarButtonType.Emoticon).rawValue) as! UIButton
            //这个name是可变的
            var name = "compose_keyboardbutton_background"
            //设置表情按钮的图标
            
            //如果是系统的,则显示的是笑脸的图标,如果是笑脸图标显示的是系统图标
            if isSystemKeyBoard {
                
                name = "compose_emoticonbutton_background"
                
            }
            
            //设置按钮的状态
            button.setImage(UIImage(named: name), forState: .Normal)
            button.setImage(UIImage(named: "\(name)_highlighted"), forState: .Highlighted)
            
            
        }
        
        
        
        
    }
    
    //创建一个闭包的属性
    var buttonClickClosure: ((type: CZComposeToolBarButtonType)->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        //设置子空间相等,并且填充父控件
        distribution = .FillEqually
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        //向stackView里面添加按钮
        
        //注意添加控件的顺序
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: CZComposeToolBarButtonType.Trend)

        addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: CZComposeToolBarButtonType.Add)
    }
    
    //通过图片的名字添加button
    
    private func addChildButton(imageName: String,type:CZComposeToolBarButtonType){
        
        let button = UIButton()
        
        button.addTarget(self, action: "didClickChildButton:", forControlEvents: .TouchUpInside)
        
        //设置图片的背景
        
        //注意这里枚举装成type 使用的方法为 type.rawValue,也就是枚举类型.rawValue
        button.tag = type.rawValue
        
        
        
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Highlighted)
        
        addArrangedSubview(button)
        
    }
    
    @objc private func didClickChildButton(button: UIButton){
        
        //回调闭包,执行对应的方法
        
//        buttonClickClosure(type)
        
        
        //这里就是int转成type模式
        buttonClickClosure?(type: CZComposeToolBarButtonType(rawValue: button.tag)!)
        
        
    }

}
